bitkeeper revision 1.624 (3fbdff8cjOXXJYub36D6Ko7MDfqOsA)
authorkaf24@scramble.cl.cam.ac.uk <kaf24@scramble.cl.cam.ac.uk>
Fri, 21 Nov 2003 12:05:32 +0000 (12:05 +0000)
committerkaf24@scramble.cl.cam.ac.uk <kaf24@scramble.cl.cam.ac.uk>
Fri, 21 Nov 2003 12:05:32 +0000 (12:05 +0000)
XenoUtil.py:
  More XenoUtil funcs.

tools/xc/py/XenoUtil.py

index f9b8728290331c98525961e354022b0f1ce7fa77..1a135bfdffb4526c0114ca680941236049b0f58a 100644 (file)
@@ -64,3 +64,67 @@ def lookup_blkdev_partn_info(partition):
                      string.atol(m.group(2)),
                      m.group(3) )
     return None
+
+
+def get_current_ipaddr(dev='eth0'):
+    """Return a string containing the primary IP address for the given
+    network interface (default 'eth0').
+    """
+    fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
+    lines = fd.readlines()
+    for line in lines:
+        m = re.search( '^\s+inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
+                       line )
+        if m:
+            return m.group(1)
+    return None
+
+def get_current_ipmask(dev='eth0'):
+    """Return a string containing the primary IP netmask for the given
+    network interface (default 'eth0').
+    """
+    fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
+    lines = fd.readlines()
+    for line in lines:
+        m = re.search( '^.+Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
+                       line )
+        if m:
+            return m.group(1)
+    return None
+
+def get_current_ipgw(dev='eth0'):
+    """Return a string containing the IP gateway for the given
+    network interface (default 'eth0').
+    """
+    fd = os.popen( '/sbin/route -n' )
+    lines = fd.readlines()
+    for line in lines:
+        m = re.search( '^\S+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' +
+                       '\s+\S+\s+\S*G.*' + dev + '.*', line )
+        if m:
+            return m.group(1)
+    return None
+
+def setup_vfr_rules_for_vif(dom,vif,addr):
+    """Takes a tuple ( domain-id, vif-id, ip-addr ), where the ip-addr
+    is expressed as a textual dotted quad, and set up appropriate routing
+    rules in Xen. No return value.
+    """
+    fd = os.open( '/proc/xeno/vfr', os.O_WRONLY )
+    if ( re.search( '169\.254', addr) ):
+        os.write( fd, 'ADD ACCEPT srcaddr=' + addr +
+                  ' srcaddrmask=255.255.255.255' +
+                  ' srcdom=' + str(dom) + ' srcidx=' + str(vif) +
+                  ' dstdom=0 dstidx=0 proto=any\n' )
+    else:
+        os.write( fd, 'ADD ACCEPT srcaddr=' + addr +
+                  ' srcaddrmask=255.255.255.255' +
+                  ' srcdom=' + str(dom) + ' srcidx=' + str(vif) +
+                  ' dst=PHYS proto=any\n' )
+    os.write( fd, 'ADD ACCEPT dstaddr=' + addr +
+              ' dstaddrmask=255.255.255.255' +
+              ' src=ANY' +
+              ' dstdom=' + str(dom) + ' dstidx=' + str(vif) +
+              ' proto=any\n' )
+    os.close( fd )
+    return None